using System;
using System.Linq;
using System.Collections.Generic;
using PhoneCore.Framework;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.Diagnostic.Tracing;
using PhoneCore.Framework.IoC;
using PhoneCore.Framework.Views.Command;
using SecureBox.UI.Infrastructure;
using SecureBox.UI.Infrastructure.Data;

namespace SecureBox.UI.ViewModel
{
   /// <summary>
   /// Startup page
   /// </summary>
    public class StartupViewPageModel : ViewModelBase
    {
        [Dependency]
        public IDataContextService DataContextService { get; set; }

        /// <summary>
        /// Initializes a new instance of the StartupViewPageModel class.
        /// </summary>
        public StartupViewPageModel()
        {
            GoTo = new ExecuteCommand<object>(DoGoTo);
            ProductVersion = App.Version;
        }

       private IEnumerable<Infrastructure.Model.Template> _templates;
       public IEnumerable<Infrastructure.Model.Template> Templates
       {
           get
           {
               //lazy load of templates
               if (_templates == null)
                   _templates = DataContextService.Templates.List().ToList();
               return _templates;
           }
       }

        public string ProductVersion { get; set; }


        private void DoGoTo(object pageName)
        {
            try
            {
                string pageNameString = String.Empty;
                if (pageName is string)
                    pageNameString = (string) pageName;
                else
                    //TODO: ugly trick with binding
                    pageNameString =
                        (string)((pageName as System.Windows.Data.Binding).Source as SecureBox.Controls.Lists.DataContextProxy).
                            DataSource;

                NavigationService.NavigateTo(pageNameString);
            }
            catch
            {
                System.Windows.MessageBox.Show(String.Format("Under construction: {0}", pageName));
            }
        }


       #region Commands

        public ExecuteCommand<object> GoTo
        {
            get;
            private set;
        }

        #endregion
    }
}